package com.niiwoo.civet.account.service.local.check;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.dto.request.CheckFileConfirmRequestDTO;
import com.niiwoo.civet.account.dto.response.CheckFileConfirmResponseDTO;
import com.niiwoo.civet.account.service.local.check.job.enums.CheckConfirmEnum;
import com.niiwoo.tripod.lanmao.component.LanMaoDirectService;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.lanmao.request.CheckFileConfirmRequest;
import com.niiwoo.tripod.lanmao.response.CheckFileConfirmResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AccountCheckingConfirmService {
	@Autowired
	private LanMaoDirectService lanMaoDirectService;

	@Autowired
	private LanMaoSequence lanMaoSequence;

	public CheckFileConfirmResponseDTO checkFileConfirm(CheckFileConfirmRequestDTO checkFileConfirmRequestDTO) {
		CheckFileConfirmRequest checkFileConfirmRequest = new CheckFileConfirmRequest();
		checkFileConfirmRequest.setRequestNo(checkFileConfirmRequestDTO.getRequestNo());
		checkFileConfirmRequest.setFileDate(checkFileConfirmRequestDTO.getFileDate());
		checkFileConfirmRequest.setDetail(checkFileConfirmRequestDTO.getDetail());

		CompletableFuture<CheckFileConfirmResponse> checkFileConfirmResponses = lanMaoDirectService
				.checkFileConfirm(checkFileConfirmRequest);

		CheckFileConfirmResponseDTO checkFileConfirmResponseDTO = new CheckFileConfirmResponseDTO();
		try {
			CheckFileConfirmResponse checkFileConfirmResponse = checkFileConfirmResponses.get();
			if (checkFileConfirmResponse.isSuccess()) {
				checkFileConfirmResponseDTO.setCode(checkFileConfirmResponse.getCode());
				checkFileConfirmResponseDTO.setStatus(Boolean.TRUE.toString());
				return checkFileConfirmResponseDTO;
			} else {
				checkFileConfirmResponseDTO.setCode(checkFileConfirmResponse.getCode());
				checkFileConfirmResponseDTO.setStatus(Boolean.FALSE.toString());
				checkFileConfirmResponseDTO.setErrorCode(checkFileConfirmResponse.getErrorCode());
				checkFileConfirmResponseDTO.setErrorMessage(checkFileConfirmResponse.getErrorMessage());
				return checkFileConfirmResponseDTO;
			}
		} catch (Exception e) {
			log.error("懒猫文件对账调用异常:{}", JSON.toJSONString(checkFileConfirmRequestDTO), e);
			checkFileConfirmResponseDTO.setCode(Byte.valueOf("1"));
			checkFileConfirmResponseDTO.setStatus(Boolean.FALSE.toString());
			checkFileConfirmResponseDTO.setErrorCode("");
			checkFileConfirmResponseDTO.setErrorMessage("第三方调用异常");
			return checkFileConfirmResponseDTO;
		}
	}

	public CheckFileConfirmResponseDTO checkFileConfirmForFileDate(String fileDate) {
		CheckFileConfirmRequest checkFileConfirmRequest = new CheckFileConfirmRequest();
		checkFileConfirmRequest.setFileDate(fileDate);

		List<Map<String, String>> detail = new ArrayList<Map<String, String>>();
		CheckConfirmEnum[] confirmTypes = CheckConfirmEnum.values();
		for (CheckConfirmEnum dl : confirmTypes) {
			Map<String, String> dt = new HashMap<String, String>();
			dt.put("fileType", dl.getCode());
			detail.add(dt);
		}
		checkFileConfirmRequest.setRequestNo(lanMaoSequence.getRequestNo());
		checkFileConfirmRequest.setDetail(detail);

		CompletableFuture<CheckFileConfirmResponse> checkFileConfirmResponses = lanMaoDirectService
				.checkFileConfirm(checkFileConfirmRequest);

		CheckFileConfirmResponseDTO checkFileConfirmResponseDTO = new CheckFileConfirmResponseDTO();
		try {
			CheckFileConfirmResponse checkFileConfirmResponse = checkFileConfirmResponses.get();
			if (checkFileConfirmResponse.isSuccess()) {
				checkFileConfirmResponseDTO.setCode(Byte.valueOf(checkFileConfirmResponse.getCode()));
				checkFileConfirmResponseDTO.setStatus(Boolean.TRUE.toString());
				return checkFileConfirmResponseDTO;
			} else {
				checkFileConfirmResponseDTO.setCode(Byte.valueOf(checkFileConfirmResponse.getCode()));
				checkFileConfirmResponseDTO.setStatus(Boolean.FALSE.toString());
				checkFileConfirmResponseDTO.setErrorCode(checkFileConfirmResponse.getErrorCode());
				checkFileConfirmResponseDTO.setErrorMessage(checkFileConfirmResponse.getErrorMessage());
				return checkFileConfirmResponseDTO;
			}
		} catch (Exception e) {
			log.error("懒猫文件对账调用异常:{}", fileDate, e);
			checkFileConfirmResponseDTO.setCode(Byte.valueOf("1"));
			checkFileConfirmResponseDTO.setStatus(Boolean.FALSE.toString());
			checkFileConfirmResponseDTO.setErrorCode("");
			checkFileConfirmResponseDTO.setErrorMessage("第三方调用异常");
			return checkFileConfirmResponseDTO;
		}
	}
}
